// pages/wrong-home/index.js
import {
    request_post
} from '../../utils/request';
const app = getApp()

Page({
    data: {
        removeOnAnswerRight: true,
        totalAnsweredQuestionTimes: 0,
        totalAnsweredWrongQuestionTimes: 0,
        todayAnsweredWrongQuestionTimes: 0,
        totalWrongPercent: 0,
        wrongBookQuestionCount: {},
        kemu: [],
        hasWrongQuestion: false,
        user: null
    },
    onShow() {
        if (app.globalData.student) {
            this.setData({
                removeOnAnswerRight: app.globalData.student.removeOnAnswerRight,
                kemu: app.globalData.student.kemu || [],
                user: app.globalData.student
            })
        }

        request_post('/exam/wrong/statistics').then(res => {
            this.setData({
                totalAnsweredQuestionTimes: res.totalAnsweredQuestionTimes,
                totalAnsweredWrongQuestionTimes: res.totalAnsweredWrongQuestionTimes,
                todayAnsweredWrongQuestionTimes: res.todayAnsweredWrongQuestionTimes,
                totalWrongPercent: res.totalWrongPercent,
                wrongBookQuestionCount: res.wrongBookQuestionCount,
                hasWrongQuestion: Object.keys(res.wrongBookQuestionCount).length > 0
            })
        })
    },
    changeRemoveOnAnswerRight() {
        request_post('/student/change-remove-on-answer-right', {
            removeOnAnswerRight: !this.data.removeOnAnswerRight
        }).then(res => {
            app.globalData.student.removeOnAnswerRight = !this.data.removeOnAnswerRight
            this.setData({
                removeOnAnswerRight: app.globalData.student.removeOnAnswerRight,
            })
        })
    },
    jumpToWrongExamQuestion(event) {
        wx.navigateTo({
            url: '/pages/exam-question/index?fromWrongBook=true&type=' + event.currentTarget.dataset.type + '&code=' + event.currentTarget.dataset.code
        })
    }
})